package com.migu.contacts.main.create;

import static com.migu.contacts.net.Service.API;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.CenterPopup;
import com.frank.libandroid.DataStore;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.util.ImageUtil;
import com.lxj.xpopup.XPopup;
import com.migu.contacts.bean.ContactBean;
import com.migu.contacts.bean.OcrBean;
import com.migu.contacts.bean.OcrReq;
import com.migu.contacts.databinding.ActivityRealScanBinding;
import com.migu.contacts.databinding.PopScanResBinding;
import com.migu.contacts.databinding.PopScanSucBinding;
import com.migu.contacts.main.ContactEditActivity;

import java.util.ArrayList;
import java.util.List;

public class RealScanActivity extends BaseActivity<ActivityRealScanBinding> {


    String url = get();


    public void shot(View view) {
    }

    @Override
    protected void initData() {
        super.initData();
        ImageUtil.loadUrl(mBinding.previewView, url);
        ocr();
    }

    private void ocr() {
        Log.d(TAG, "ocr() called with: ");
        String s = url;
        Log.i(TAG, "ocr: " + s);
        OcrReq ocrReq = new OcrReq();
        ocrReq.url = s;
        API.ocr(ocrReq).watch(mActivity, new HttpCallback<OcrBean>() {
            @Override
            protected void onSuccess(OcrBean ossToken) {
                Log.d(TAG, "onSuccess() called with: ossToken = [" + ossToken + "]");
                if (ossToken == null || TextUtils.isEmpty(ossToken.name)) {
                    onError(-1, "");
                    return;
                }
                ContactBean contactBean = new ContactBean();
                contactBean.realName = ossToken.name;

                List<String> telCell = ossToken.telCell;
                if (telCell != null && !telCell.isEmpty()) {
                    contactBean.mobile = telCell.get(0);
                }
                contactBean.jobList = new ArrayList<>();
                ContactBean.JobList jobList = new ContactBean.JobList();
                contactBean.jobList.add(jobList);
                List<String> company = ossToken.company;
                if (company != null && !company.isEmpty()) {
                    String s1 = company.get(0);
                    jobList.name = s1;
                }
                List<String> title = ossToken.title;
                if (title != null && !title.isEmpty()) {
                    String s1 = title.get(0);
                    jobList.positionName = s1;
                }
                List<String> email = ossToken.email;
                if (email != null && !email.isEmpty()) {
                    String s1 = email.get(0);
                    contactBean.email = s1;
                }
                showResult(true, contactBean);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showResult(false, null);
            }
        });
    }

    private void showResult(boolean b, ContactBean ossToken) {
        CenterPopup bottomPopup;
        if (b) {
            bottomPopup = new CenterPopup<PopScanSucBinding>(mActivity) {
            };
        } else {
            bottomPopup = new CenterPopup<PopScanResBinding>(mActivity) {
            };
        }
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
        mBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                bottomPopup.dismiss();
                if (b) {
                    DataStore.put(ContactEditActivity.CONTACT_BEAN_KEY, ossToken);
                    start(ContactCreateActivity.class);
                }
            }
        }, 500);
    }

    public void goAlbum(View view) {
        start(OcrListActivity.class);
    }
}